package com.jerry.springcloud.service.impl;

import cn.hutool.core.util.IdUtil;
import com.jerry.springcloud.dao.PaymentDao;
import com.jerry.springcloud.entity.Payment;
import com.jerry.springcloud.service.PaymentService;
import com.netflix.hystrix.contrib.javanica.annotation.DefaultProperties;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.concurrent.TimeUnit;

@Service
@DefaultProperties(defaultFallback = "paymentInfo_Global_Handler")
public class PaymentServiceImpl implements PaymentService {

    @Resource
    private PaymentDao paymentDao;

    @Override
    public int create(Payment payment) {
        return paymentDao.create(payment);
    }

    @Override
    public Payment getPaymentById(Long id) {
        return paymentDao.getPaymentById(id);
    }

    @Override
    public List<Payment> getPaymentList() {
        return paymentDao.getPaymentList();
    }

    @Override
    public String paymentInfo_OK(Long id) {
        StringBuffer sb = new StringBuffer();
        sb.append("paymentInfo_OK ******");
        sb.append("线程池=== "+Thread.currentThread().getName()+"******");
        sb.append("id==="+id);
        return sb.toString();
    }

    @HystrixCommand
    @Override
    public String paymentInfo_Error(Long id) {

        int age = 10/0;  //错误

        StringBuffer sb = new StringBuffer();
        sb.append("paymentInfo_Error ****** ");
        sb.append("线程池=== "+Thread.currentThread().getName() +"******");
        sb.append("id==="+id);
        return sb.toString();
    }

    @HystrixCommand(commandProperties = {
            @HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds", value = "3000")
    })
    @Override
    public String paymentInfo_Timeout(Long id) {
        int timeNumber = 5;
        try {
            TimeUnit.SECONDS.sleep(timeNumber);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        StringBuffer sb = new StringBuffer();
        sb.append("paymentInfo_Timeout ****** ");
        sb.append("线程池=== "+Thread.currentThread().getName() +"******");
        sb.append("id=== "+id +"******");
        sb.append("耗时==="+timeNumber);
        return sb.toString();
    }

    public String paymentInfo_Global_Handler() {
        StringBuffer sb = new StringBuffer();
        sb.append("提供者 ****** paymentInfo_Global_Handler ****** ");
        sb.append("线程池=== "+Thread.currentThread().getName());
        return sb.toString();
    }

    @HystrixCommand(fallbackMethod = "paymentInfo_CircuitBreaker_Handler", commandProperties = {
            @HystrixProperty(name = "circuitBreaker.enabled", value = "true"),  // 开启断路器
            @HystrixProperty(name = "circuitBreaker.requestVolumeThreshold", value = "10"),  // 请求次数。超过峰值，熔断将会从关闭进入开启
            @HystrixProperty(name = "circuitBreaker.sleepWindowInMilliseconds", value = "10000"),  // 快照时间窗
            @HystrixProperty(name = "circuitBreaker.errorThresholdPercentage", value = "60"),  // 失败率达到多少后跳闸
    })
    @Override
    public String paymentInfo_CircuitBreaker(Long id) {
        if (id < 0) {
            throw new RuntimeException("****** id 不能为负数 ******");
        }
        String serialNumber = IdUtil.simpleUUID();   //相当于 UUID.randomUUID().toString()

        StringBuffer sb = new StringBuffer();
        sb.append("paymentInfo_CircuitBreaker ****** ");
        sb.append("线程池=== "+Thread.currentThread().getName() +"******");
        sb.append("id=== "+id +"******");
        sb.append("调用成功，流水号==" + serialNumber);
        return sb.toString();
    }

    public String paymentInfo_CircuitBreaker_Handler(Long id) {
        return "id 不能为负数，请稍后再试！id="+id;
    }

}
